/*
    Copyright Nicolas Ronvel 2007
	gulix33xp@yahoo.fr

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

using System;
using System.Drawing;

using SdlDotNet.Graphics;
using Sdl = SdlDotNet.Graphics;
using SdlDotNet.Core;
using SdlDotNet.Input;

using BlindShark.GameElements;
using BlindShark;

namespace BlindShark.GameStates
{
	/// <summary>
	/// Displays a Menu
	/// </summary>
	public class GS_Menu : GameState
	{
		#region Variables
		Surface _sfFrame;
		
		// Fonts
		string _sFont;
		Sdl.Font _fTitle;
		Sdl.Font _fMenuSelected;
		Sdl.Font _fMenuUnselected;
		
		// Strings to display
		protected string _sTitle;
		protected string[] _sMenuEntries;
		
		// Elements location
		Rectangle _rctFrame;
		Rectangle _rctTitle;
		Rectangle[] _rctMenuEntries;
		
		protected int _iMenuSelected;
		
		#endregion
				
		#region Constructors
		protected GS_Menu()
		{
			_sFont = "fonts/Fertigo_17b.otf";
			
			// Initializing the screen title
			_fTitle = new Sdl.Font(_sFont, 36);
			_fTitle.Underline = true;
									
			// Initializing fonts
			_fMenuSelected = new Sdl.Font(_sFont, 30);
			_fMenuSelected.Underline = true;
			_fMenuUnselected = new Sdl.Font(_sFont, 30);
			
			Keyboard.KeyRepeat = false;
			
			// By default, the first element is selected
			_iMenuSelected = 0;
		}
		#endregion
		
		#region GameState interface members
		public void Draw()
		{
			ScreenManager.MainScreen.Blit(_sfFrame, new Point(150,50));
			
			// Displaying menu entries
			for(int i=0; i<_sMenuEntries.Length; i++)
			{
				if (_iMenuSelected==i)
					ScreenManager.MainScreen.Blit(_fMenuSelected.Render(_sMenuEntries[i], Color.Green), 
					                              _rctMenuEntries[i].Location);
				else
					ScreenManager.MainScreen.Blit(_fMenuUnselected.Render(_sMenuEntries[i], Color.Orange), 
					                              _rctMenuEntries[i].Location);
			}
		}
		

		public void CleanUp()
		{
			_sfFrame = null;
			Events.Remove();
		}
		
		public void HandleEvents()
		{
			while (Events.Poll())
			{
				// The Key Arrows change the selected menu
				if (Keyboard.IsKeyPressed(Key.DownArrow) && (_iMenuSelected<_sMenuEntries.Length-1))
					_iMenuSelected++;
				else if (Keyboard.IsKeyPressed(Key.UpArrow) && (_iMenuSelected>0))
					_iMenuSelected--;
				// Enter & Space valid the choice
				else if (Keyboard.IsKeyPressed(Key.Return) || Keyboard.IsKeyPressed(Key.Space))
				{
					ValidChoice();
				}
				// If the user click on a menu entry, it selects it
				// If it is already selected, it valids it
				if (Mouse.IsButtonPressed(MouseButton.PrimaryButton))
				{
					for(int i=0; i<_rctMenuEntries.Length; i++)
					{
						if (_rctMenuEntries[i].Contains(Mouse.MousePosition))
						{
							if (i==_iMenuSelected)
								ValidChoice();
							else
								_iMenuSelected = i;
						}
					}
				}
			}
		}
		
		public void Init()
		{
			// Loading Background
			ScreenManager.MainScreen.Blit(SurfaceManager.GetSurface("gfx/background.png"));
			
			// Initializing Frame
			_sfFrame = new Surface(SurfaceManager.GetSurface("gfx/background_infoWindow.png").Size);
			_sfFrame.Blit(SurfaceManager.GetSurface("gfx/background_infoWindow.png"));
			_rctFrame = new Rectangle(new Point(150,50), _sfFrame.Size);
			
			// Displaying title on the frame
			_rctTitle = new Rectangle(new Point(0,20),
			                            _fTitle.SizeText(_sTitle));
			_rctTitle.X = (_sfFrame.Width - _rctTitle.Width)/2;
			_sfFrame.Blit(_fTitle.Render(_sTitle, Color.OrangeRed),_rctTitle.Location);
			
			// Initializing menu entries
			_rctMenuEntries = new Rectangle[_sMenuEntries.Length];
			int iBottom = _rctTitle.Bottom + 30;
			for(int i=0; i<_rctMenuEntries.Length; i++)
			{
				_rctMenuEntries[i].Size = _fMenuSelected.SizeText(_sMenuEntries[i]);
				_rctMenuEntries[i].Y = _rctFrame.Y + iBottom;
				_rctMenuEntries[i].X = _rctFrame.X + (_rctFrame.Width-_rctMenuEntries[i].Width)/2;
				iBottom = _rctMenuEntries[i].Bottom + 30;
			}
		}
		
		public void Update()
		{
			// No Update Actions
		}
		#endregion
		
		protected virtual void ValidChoice()
		{
			
		}
	}
}
